package com.prsistema.Fornecedores;

import com.prsistema.DAO.Conexao;
import com.prsistema.DAO.DAOFornecedor;
import com.prsistema.utilitarias.Empresa;
import java.util.ArrayList;
import javax.swing.JOptionPane;

public class VisualizarFornecedor extends javax.swing.JPanel {

    private ArrayList<Empresa> listaFornecedores = new ArrayList<Empresa>();
    private DAOFornecedor daoFornecedor;
    private final String urlBtnImagem = "/com/prsistema/ImagensIcones/";
    //Variáveis que determinam as ações dos botões novo, atualiza
    //que quando acionados mudam os valores desses booleanos e executam os comando salvar e cancelar
    private boolean novo, salvar, cancelar;

    
    private final String[] listaEstados = {"", "AC", "AL", "AP", "AM", "BA", "CE", "DF", "ES", "GO", "MA", "MG", "MS", "MT", "PA", "PB", "PE", "PI", 
        "PR", "RJ", "RN", "RO", "RR", "RS", "SC", "SE", "SP", "TO"};

    public VisualizarFornecedor(Conexao con) {
        this.daoFornecedor = new DAOFornecedor(con.getConector());
        this.listaFornecedores = this.daoFornecedor.selectAll();
        //Inicializando para que os botões começem com a função de novo e atualizar
        this.novo = true;
        this.salvar = false;
        this.cancelar = false;
        initComponents();
        this.carregarDados();
    }

    private void carregarDados() {
        this.listaFornecedores = this.daoFornecedor.selectAll();
        this.preencheCampos();
    }

    private void preencheCampos() {
        this.listagemFornecedores.removeAll();
        for (int i = 0; i < this.listaFornecedores.size(); i++) {
            listagemFornecedores.add(listaFornecedores.get(i).getNome());
        }
        
        for(int i = 0; i<this.listaEstados.length;i++){
            this.cbEstado.add(this.listaEstados[i]);
        }
    }

    //Método chamado quando outro produto é selecionado na lista ou selecionado pelos botões de controle
    //mostrando na telas todas as informações do produto selecionado
    private void mudaPosicao(int index) {
        Empresa forn = this.listaFornecedores.get(index);

        this.txtCodigo.setText(String.valueOf(forn.getId()));
        this.txtNome.setText(forn.getNome());
        this.txtCnpj.setText(forn.getCnpj());
        this.txtInscEstadual.setText(forn.getInscEstadual());
        this.txtCep.setText(forn.getCep());
        this.cbEstado.select(forn.getEstado());
        this.txtMunicipio.setText(forn.getMunicipio());
        this.txtEndereco.setText(forn.getEndereco());
        this.txtBairro.setText(forn.getBairro());
        this.txtComplemento.setText(forn.getComplemento());
        this.txtEmail.setText(forn.getEmail());
        this.txtTelefone.setText(forn.getTelefone());
        this.txtCelular.setText(forn.getCelular());
        this.txtResponsavel.setText(forn.getResponsavel());
        this.txtInfoAdd.setText(forn.getInfoAdd());

        this.btnAtualizar.setEnabled(true);
        this.btnDeletar.setEnabled(true);
    }

    private void limpaCampos() {
        //Deixa todos os campos em branco
        this.txtCodigo.setText("");
        this.txtNome.setText("");
        this.txtCnpj.setText("");
        this.txtInscEstadual.setText("");
        this.txtCep.setText("");
        this.cbEstado.select("");
        this.txtMunicipio.setText("");
        this.txtEndereco.setText("");
        this.txtBairro.setText("");
        this.txtComplemento.setText("");
        this.txtEmail.setText("");
        this.txtTelefone.setText("");
        this.txtCelular.setText("");
        this.txtResponsavel.setText("");
        this.txtInfoAdd.setText("");
    }

    private void abilitaCampos(boolean abilita) {
        //Desabilita campos de texto
        this.txtNome.setEditable(abilita);
        this.txtCnpj.setEditable(abilita);
        this.txtInscEstadual.setEditable(abilita);
        this.txtCep.setEditable(abilita);
        this.cbEstado.setEnabled(abilita);
        this.txtMunicipio.setEditable(abilita);
        this.txtEndereco.setEditable(abilita);
        this.txtBairro.setEditable(abilita);
        this.txtComplemento.setEditable(abilita);
        this.txtEmail.setEditable(abilita);
        this.txtTelefone.setEditable(abilita);
        this.txtCelular.setEditable(abilita);
        this.txtResponsavel.setEditable(abilita);
        this.txtInfoAdd.setEditable(abilita);
    }

    private void cancelar() {
        this.limpaCampos();

        //Abilita botões 
        this.btnAnterior.setEnabled(true);
        this.btnProximo.setEnabled(true);
        this.listagemFornecedores.setEnabled(true);
        this.btnAtualizar.setEnabled(false);

        this.abilitaCampos(false);

        //Muda status de salvar para novo
        this.btnNovo.setText("Novo");
        this.btnNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource(this.urlBtnImagem + "btnAdicionar.png")));
        this.btnAtualizar.setText("Atualizar");
        this.btnAtualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource(this.urlBtnImagem + "btnAtualizar1.png")));

        this.carregarDados();
    }

    private boolean salvar() {
        boolean retorno = false;

        try {
            Empresa forn = new Empresa(); //Produto que está sendo cadastrado

            //Passando as informações dos campos preenchidos para o objeto prod
            forn.setNome(this.txtNome.getText());
            forn.setCnpj(this.txtCnpj.getText());
            forn.setInscEstadual(this.txtInscEstadual.getText());
            forn.setCep(this.txtCep.getText());
            forn.setEstado(this.cbEstado.getSelectedItem());
            forn.setMunicipio(this.txtMunicipio.getText());
            forn.setEndereco(this.txtEndereco.getText());
            forn.setBairro(this.txtBairro.getText());
            forn.setComplemento(this.txtComplemento.getText());
            forn.setEmail(this.txtEmail.getText());
            forn.setTelefone(this.txtTelefone.getText());
            forn.setCelular(this.txtCelular.getText());
            forn.setResponsavel(this.txtResponsavel.getText());
            forn.setInfoAdd(this.txtInfoAdd.getText());

            //É neste if que devem ser feitas as comparações para que os campos obrigatórios não estejam vazios
            if ("".equals(forn.getNome())) {
                JOptionPane.showMessageDialog(null, "Preencha os campos obrigatórios");
            } else {

                //Este if verifica qual é a ação do botão no momento, cadastrar ou atualizar
                if (this.novo) {

                    if (this.daoFornecedor.insert(forn)) {
                        JOptionPane.showMessageDialog(null, "Cadastrado com sucesso");
                        retorno = true;
                        this.cancelar();
                    }

                } else {
                    forn.setId(Integer.parseInt(this.txtCodigo.getText()));

                    if (this.daoFornecedor.update(forn)) {
                        JOptionPane.showMessageDialog(null, "Atualizado com sucesso");
                        retorno = true;
                        this.cancelar();
                    }
                }
            }

            return retorno;
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);
            return retorno;
        }
    }

    private void novoAtuali() {

        //Comparação que verifica se um produto novo esta sendo cadastrado ou se um ja existente está sendo atualizado
        //Caso esteja sendo atualizado, os campos continuaram preenchidos com as informações do produto selecionado
        //Caso seja para cadastro, todos os campos ficam vazios
        if (this.novo) {
            //Deseleciona item na listagem
            this.listagemFornecedores.deselect(this.listagemFornecedores.getSelectedIndex());

            this.btnAtualizar.setEnabled(true);

            this.limpaCampos();
        } 

        //desabilitando todos os elementos que não participam do processo de cadastro ou atualização
        this.btnAnterior.setEnabled(false);
        this.btnProximo.setEnabled(false);
        this.btnDeletar.setEnabled(false);
        this.listagemFornecedores.setEnabled(false);

        //Abilitando os campos para que o usuário possa informar as informações que deseja
        this.abilitaCampos(true);

        //Muda status de novo para salvar
        this.btnNovo.setText("Salvar");
        this.btnNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource(this.urlBtnImagem + "btnSalvar.png")));
        this.btnAtualizar.setText("Cancelar");
        this.btnAtualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource(this.urlBtnImagem + "btnCancelar.png")));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        listagemFornecedores = new java.awt.List();
        jPanel4 = new javax.swing.JPanel();
        btnAnterior = new javax.swing.JButton();
        btnProximo = new javax.swing.JButton();
        btnNovo = new javax.swing.JButton();
        btnAtualizar = new javax.swing.JButton();
        btnDeletar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        txtInscEstadual = new javax.swing.JTextField();
        txtCnpj = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtTelefone = new javax.swing.JTextField();
        txtNome = new javax.swing.JTextField();
        txtBairro = new javax.swing.JTextField();
        txtResponsavel = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtEndereco = new javax.swing.JTextField();
        txtEmail = new javax.swing.JTextField();
        txtCelular = new javax.swing.JTextField();
        txtCep = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtMunicipio = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        txtComplemento = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtInfoAdd = new javax.swing.JTextArea();
        jLabel16 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        cbEstado = new java.awt.Choice();
        jLabel14 = new javax.swing.JLabel();

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "FORNECEDORES CADASTRADOS", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Calibri", 0, 13))); // NOI18N
        jPanel3.setPreferredSize(new java.awt.Dimension(240, 460));

        listagemFornecedores.setFont(new java.awt.Font("Calibri", 0, 13));
        listagemFornecedores.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                listagemFornecedoresItemStateChanged(evt);
            }
        });
        listagemFornecedores.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                listagemFornecedoresActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(listagemFornecedores, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(listagemFornecedores, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "BOTÕES DE CONTROLE", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Calibri", 0, 13))); // NOI18N

        btnAnterior.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/prsistema/ImagensIcones/btnSetaAnterior.png"))); // NOI18N
        btnAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnteriorActionPerformed(evt);
            }
        });

        btnProximo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/prsistema/ImagensIcones/btnSetaProximo.png"))); // NOI18N
        btnProximo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProximoActionPerformed(evt);
            }
        });

        btnNovo.setFont(new java.awt.Font("Calibri", 0, 13));
        btnNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/prsistema/ImagensIcones/btnAdicionar.png"))); // NOI18N
        btnNovo.setText("Novo");
        btnNovo.setMaximumSize(new java.awt.Dimension(105, 35));
        btnNovo.setMinimumSize(new java.awt.Dimension(105, 35));
        btnNovo.setPreferredSize(new java.awt.Dimension(105, 37));
        btnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoActionPerformed(evt);
            }
        });

        btnAtualizar.setFont(new java.awt.Font("Calibri", 0, 13));
        btnAtualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/prsistema/ImagensIcones/btnAtualizar1.png"))); // NOI18N
        btnAtualizar.setText("Atualizar");
        btnAtualizar.setEnabled(false);
        btnAtualizar.setMaximumSize(new java.awt.Dimension(105, 35));
        btnAtualizar.setMinimumSize(new java.awt.Dimension(105, 35));
        btnAtualizar.setPreferredSize(new java.awt.Dimension(105, 37));
        btnAtualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAtualizarActionPerformed(evt);
            }
        });

        btnDeletar.setFont(new java.awt.Font("Calibri", 0, 13));
        btnDeletar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/prsistema/ImagensIcones/btnExcluir.png"))); // NOI18N
        btnDeletar.setText("Deletar");
        btnDeletar.setEnabled(false);
        btnDeletar.setMaximumSize(new java.awt.Dimension(105, 35));
        btnDeletar.setMinimumSize(new java.awt.Dimension(105, 35));
        btnDeletar.setPreferredSize(new java.awt.Dimension(105, 37));
        btnDeletar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeletarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnAnterior, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnNovo, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnAtualizar, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnDeletar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnProximo, javax.swing.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnProximo, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
                    .addComponent(btnAtualizar, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
                    .addComponent(btnDeletar, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
                    .addComponent(btnAnterior, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnNovo, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE))
                .addContainerGap())
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "INFORMAÇÕES DE FORNECEDOR", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Calibri", 0, 13))); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(571, 356));

        txtInscEstadual.setEditable(false);
        txtInscEstadual.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtInscEstadualActionPerformed(evt);
            }
        });

        txtCnpj.setEditable(false);

        jLabel6.setFont(new java.awt.Font("Calibri", 0, 13));
        jLabel6.setText("Bairro");

        txtTelefone.setEditable(false);

        txtNome.setEditable(false);

        txtBairro.setEditable(false);

        txtResponsavel.setEditable(false);

        jLabel10.setFont(new java.awt.Font("Calibri", 0, 13));
        jLabel10.setText("Responsável");

        jLabel4.setFont(new java.awt.Font("Calibri", 0, 13));
        jLabel4.setText("CEP");

        jLabel11.setFont(new java.awt.Font("Calibri", 0, 13));
        jLabel11.setText("Telefone");

        jLabel1.setFont(new java.awt.Font("Calibri", 0, 13));
        jLabel1.setText("Razão Social");

        txtEndereco.setEditable(false);

        txtEmail.setEditable(false);

        txtCelular.setEditable(false);

        txtCep.setEditable(false);

        jLabel8.setFont(new java.awt.Font("Calibri", 0, 13));
        jLabel8.setText("Município");

        jLabel9.setFont(new java.awt.Font("Calibri", 0, 13));
        jLabel9.setText("Email");

        jLabel5.setFont(new java.awt.Font("Calibri", 0, 13));
        jLabel5.setText("Endereço");

        jLabel2.setFont(new java.awt.Font("Calibri", 0, 13));
        jLabel2.setText("CNPJ");

        jLabel12.setFont(new java.awt.Font("Calibri", 0, 13));
        jLabel12.setText("Celular");

        jLabel3.setFont(new java.awt.Font("Calibri", 0, 13));
        jLabel3.setText("Insc. Estadual");

        txtMunicipio.setEditable(false);

        jLabel7.setFont(new java.awt.Font("Calibri", 0, 13));
        jLabel7.setText("Estado");

        jLabel13.setFont(new java.awt.Font("Calibri", 0, 13));
        jLabel13.setText("Complemento");

        txtComplemento.setEditable(false);

        jLabel15.setFont(new java.awt.Font("Calibri", 0, 13));
        jLabel15.setText("Informações Adicionais");

        txtInfoAdd.setColumns(20);
        txtInfoAdd.setEditable(false);
        txtInfoAdd.setRows(5);
        jScrollPane1.setViewportView(txtInfoAdd);

        jLabel16.setFont(new java.awt.Font("Calibri", 0, 13));
        jLabel16.setText("Código");

        txtCodigo.setEditable(false);

        cbEstado.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel11)
                    .addComponent(jLabel13)
                    .addComponent(jLabel1)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtEndereco, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtBairro, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel15, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel16)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(txtNome, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtCnpj, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtInscEstadual, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(txtTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel12)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCelular, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtResponsavel, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtComplemento, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtCep, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbEstado, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtMunicipio, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel16))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtCnpj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(txtInscEstadual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtMunicipio, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
                        .addComponent(jLabel8))
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtCep, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
                        .addComponent(jLabel7))
                    .addComponent(cbEstado, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtBairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13)
                    .addComponent(txtComplemento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(txtTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12)
                    .addComponent(txtCelular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtResponsavel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel15)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jLabel14.setFont(new java.awt.Font("Calibri", 0, 13));
        jLabel14.setText("Status...");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 573, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 471, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 345, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel14)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void listagemFornecedoresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listagemFornecedoresActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_listagemFornecedoresActionPerformed

private void btnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoActionPerformed
    //Comparação para determinar qual método será chamado, novo ou salvar
    if (this.salvar) {
        if (this.salvar()) {
            this.cancelar = false;
            this.salvar = false;
        }
    } else {
        this.salvar = true;
        this.novo = true;
        this.cancelar = true;
        this.novoAtuali();
    }
}//GEN-LAST:event_btnNovoActionPerformed

private void btnAtualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAtualizarActionPerformed
    //Comparação para determinar qual método será chamado, atualizar ou cancelar
    if (this.cancelar) {
        this.cancelar();
        this.cancelar = false;
        this.salvar = false;
    } else {
        this.novo = false;
        this.cancelar = true;
        this.salvar = true;
        this.novoAtuali();
    }
}//GEN-LAST:event_btnAtualizarActionPerformed

private void btnDeletarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeletarActionPerformed
    //Aqui é pedido a confirmação do usuário para a exclusão de algum produto
    int opt = JOptionPane.showConfirmDialog(null, "Deseja mesmo excluir?");
    
    //Caso o numero retornado seja 0, o usuário deseja mesmo deletar o produto, então tais comandos são executados
    if (opt == 0) {
        Empresa clie = this.listaFornecedores.get(this.listagemFornecedores.getSelectedIndex());
        if (this.daoFornecedor.delete(clie.getId())) {
            JOptionPane.showMessageDialog(null, "Deletado com sucesso");
            this.carregarDados();

            this.btnDeletar.setEnabled(false);
            this.limpaCampos();
            
        }
    }
}//GEN-LAST:event_btnDeletarActionPerformed

private void txtInscEstadualActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtInscEstadualActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtInscEstadualActionPerformed

private void listagemFornecedoresItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_listagemFornecedoresItemStateChanged
    //Apenas pega o indice escolhido pelo usuário e chama o método mudaPosição
    int index = this.listagemFornecedores.getSelectedIndex();
    this.mudaPosicao(index);
}//GEN-LAST:event_listagemFornecedoresItemStateChanged

private void btnAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnteriorActionPerformed
    int index = this.listagemFornecedores.getSelectedIndex();
    
    if (index == -1) {
        this.mudaPosicao(0);
        this.listagemFornecedores.select(0);
    } else {
        if (index == 0) {
            this.mudaPosicao(this.listaFornecedores.size() - 1);
            this.listagemFornecedores.select(this.listaFornecedores.size() - 1);
        } else {
            this.mudaPosicao(index - 1);
            this.listagemFornecedores.select(index - 1);
        }
    }
}//GEN-LAST:event_btnAnteriorActionPerformed

private void btnProximoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProximoActionPerformed
    int index = this.listagemFornecedores.getSelectedIndex();

    if (index == -1 || index == this.listaFornecedores.size() - 1) {
        this.mudaPosicao(0);
        this.listagemFornecedores.select(0);
    } else {
        this.mudaPosicao(index + 1);
        this.listagemFornecedores.select(index + 1);
    }
}//GEN-LAST:event_btnProximoActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAnterior;
    private javax.swing.JButton btnAtualizar;
    private javax.swing.JButton btnDeletar;
    private javax.swing.JButton btnNovo;
    private javax.swing.JButton btnProximo;
    private java.awt.Choice cbEstado;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private java.awt.List listagemFornecedores;
    private javax.swing.JTextField txtBairro;
    private javax.swing.JTextField txtCelular;
    private javax.swing.JTextField txtCep;
    private javax.swing.JTextField txtCnpj;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtComplemento;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtEndereco;
    private javax.swing.JTextArea txtInfoAdd;
    private javax.swing.JTextField txtInscEstadual;
    private javax.swing.JTextField txtMunicipio;
    private javax.swing.JTextField txtNome;
    private javax.swing.JTextField txtResponsavel;
    private javax.swing.JTextField txtTelefone;
    // End of variables declaration//GEN-END:variables
}
